import requests
from PyQt5.QtCore import pyqtSignal
from .login_ui import LoginUI
from PyQt5.QtWidgets import QMessageBox

class LoginWindow(LoginUI):
    login_success = pyqtSignal()

    def __init__(self):
        super().__init__()
        self.login_button.clicked.connect(self.handle_login)

    def handle_login(self):
        username = self.username_input.text()
        password = self.password_input.text()

        try:
            response = requests.post('http://127.0.0.1:8000/login', auth=(username, password))
            if response.status_code == 200:
                QMessageBox.information(self, '登录成功', '欢迎使用周报分析器！')
                self.login_success.emit()
                self.close()
            else:
                QMessageBox.warning(self, '登录失败', '用户名或密码错误，请重试。')
        except Exception as e:
            QMessageBox.critical(self, '错误', f'发生网络错误: {str(e)}')